package com.mordenx.action;

import com.mordenx.entity.SecKillingMerchant;
import com.mordenx.service.SecKillingMerchantService;
import com.mordenx.vo.merchant.MerchantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("secKillingMerchantAction")
public class SecKillingMerchantAction {

    @Autowired
    SecKillingMerchantService secKillingMerchantService;

    @RequestMapping("addMerchant")
    public  String addMerchant(){
        return "merchant/addMerchant";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String insertMerchant(SecKillingMerchant secKillingMerchant){
        secKillingMerchantService.insertMerchant(secKillingMerchant);
        return "redirect:queryByVo";
    }

    @RequestMapping("delete")
    public String deleteMerChant( int id){
        secKillingMerchantService.deleteMerchantById(id);
        return "redirect:queryByVo";
    }

    @RequestMapping("update")
    public String updateMerchant(HttpServletRequest httpServletRequest,int id){
        SecKillingMerchant merchant
                = secKillingMerchantService.queryById(id);
        httpServletRequest.setAttribute("merchant",merchant);
        return "merchant/updateMerchant";
    }

    @RequestMapping(value = "toUpdate",method = RequestMethod.POST)
    public String updateMerChantById(HttpServletRequest httpServletRequest, SecKillingMerchant secKillingMerchant){
        secKillingMerchantService.updateMerchantById(secKillingMerchant);
        httpServletRequest.setAttribute("merchant",secKillingMerchant);
        return "redirect:queryByVo";
    }

    @RequestMapping("query")
    public String queryById(HttpServletRequest httpServletRequest,int id){
        SecKillingMerchant secKillingMerchant= secKillingMerchantService.queryById(id);
        httpServletRequest.setAttribute("merchant",secKillingMerchant);
        return "merchant/viewMerchant";
    }

    @RequestMapping("queryByVo")
    public String queryByVo(HttpServletRequest httpServletRequest,MerchantVo merchantVo){
        List<SecKillingMerchant> merchantList =secKillingMerchantService.queryMerchantByVo(merchantVo);
        httpServletRequest.setAttribute("merchantList",merchantList);
        return "merchant/merchantList";
    }
}
